/**
 * A class that represents a cookie bakery
 * 
 */
@Author("Lajos-Zoltan Zsombori, David Schibl")
public class Bakery {
	
	OvenCircleCookie ovenCircle = null;
	OvenDoubleCookie ovenDouble = null;
	OvenMoonCookie ovenMoon = null;
	OvenSantaCookie ovenSanta = null;
	
	
	/**
	 * Creates a bakery for cookies.
	 */
	
	public Bakery(){
		ovenCircle = new OvenCircleCookie();
		ovenDouble = new OvenDoubleCookie();
		ovenMoon = new OvenMoonCookie();
		ovenSanta = new OvenSantaCookie();
	}
	
	/**
	 * Bakes an order of cookies
	 * @return a cookie box
	 */
	@Author("Lajos-Zoltan Zsombori, David Schibl")
	public CookieBox bakeOrder(Order o){
		// o != null
		CookieBox box = new CookieBox();
		
		for(Position p: o.getOrder()){
			for(int i=1;i<=p.getCount();i++) {
				box.add(bakeCookie(p));
			}
		}
		// box != null
		return box;
	}
	
	/**
	 * bakes a singleCookie
	 * @return SingleCookie
	 */
	@Author("David Schibl")
	private SingleCookie bakeSingle(Position p){
		//p != null
		SingleCookie temp=null;
		
		if (p.getShape() instanceof ShapeCircle) {
			temp=ovenCircle.bakeCookie(p.getDough());	
			
		} else if (p.getShape() instanceof ShapeMoon) {
			temp=ovenMoon.bakeCookie(p.getDough());

		} else if(p.getShape() instanceof ShapeSanta) {
			temp=ovenSanta.bakeCookie(p.getDough());
			
		}
		//temp != null
		return temp;
	}
	/**
	 * bakes a DoubleCookie
	 * @return DoubleCookie
	 */
	@Author("David Schibl")
	private DoubleCookie bakeDouble(Position p){
		// p != null
		return ovenDouble.bakeCookie (this.bakeSingle(p), p.getFill());
	}
	
	/**
	 * Bakes a Cookie
	 * @return Cookie
	 */
	@Author("David Schibl")
	private Cookie bakeCookie(Position p){
		// p != null
		if(p.getFill()==null) {
			return bakeSingle(p);
		}
		return bakeDouble(p);
	}
	
}
